<?php
namespace app\reduction\controller;
use app\AdminRest;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use app\reduction\model\Active;
use app\reduction\model\PayRecord;
use think\App;
use app\reduction\model\StaffRecord as StaffRecord;
use app\reduction\model\UserRecord as model;
use app\reduction\model\Company;
use think\facade\Db;


class AdminRecord extends AdminRest
{

    protected $model;

    protected $staff_record;

    protected $company_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new Model();

        $this->staff_record = new StaffRecord();

        $this->company_model = new Company();
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:30
     * @return void
     * descrption:员工发放记录
     */
    public function staffRecordList(){

       $input = $this->_input;
        //uniacid
       $dis[] = ['a.uniacid','=',$this->_uniacid];

       $dis[] = ['a.active_id','=',$input['active_id']];

       $where = [];
       //查询
       if(!empty($input['name'])){

           $where[] = ['b.name','like',"%".$input['name']."%"];

           $where[] = ['c.nickName','like',"%".$input['name']."%"];
       }
        //查询
       $data  = $this->staff_record->staffRecordList($dis,$where,$input['limit']);

       $active_model = new Active();

       $data['active_name'] = $active_model->where(['id'=>$input['active_id']])->value('title');

       return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:22
     * @功能说明:
     */
    public function userRecordList(){

        $input = $this->_input;
        //uniacid
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //查询
        if(!empty($input['name'])){

            $dis[] = ['b.nickName','like',"%".$input['name']."%"];
        }

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        $dis[] = ['a.active_id','=',$input['active_id']];

        if(!empty($input['staff_id'])){

            $dis[] = ['a.staff_id','=',$input['staff_id']];
        }

        //查询
        $data  = $this->model->recordList($dis,$input['limit']);

        $active_model = new Active();

        $data['active_name'] = $active_model->where(['id'=>$input['active_id']])->value('title');

        $data['staff_name']  = !empty($input['staff_id'])?lbUserName($input['staff_id']):'';

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 16:17
     * @功能说明:员工码的列表
     */
    public function staffQrList(){

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['b.is_staff','=',1];
        //员工姓名
        if(!empty($input['staff_name'])){

            $dis[] = ['a.name','like','%'.longbing_filterEmoji($input['staff_name']).'%'];
        }
        //用户昵称
        if(!empty($input['user_name'])){

            $dis[] = ['b.nickName','like','%'.longbing_filterEmoji($input['user_name']).'%'];
        }
        $where = [];
        //公司部门
        if(!empty($input['company'])){
          //转换数据
          $company = $this->company_model->changeData($input['company']);
          //公司
          $where[] = ['c.id','in',$company];
          //部门
          $where[] = ['c.top_id','in',$company];
        }
        //搜索
        $data = $this->staff_record->staffQrList($dis,$where,$input['limit']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 16:56
     * @功能说明:满减支付码
     */

    public function reductionPayQr(){

        $input= $this->_input;

        $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

        if(empty($data)){

            $this->errorMsg('生成码失败');
        }

        $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

        $qr   = $data['qr_path'];

        Db::name('longbing_card_user')->where(['id'=>$input['id']])->update(['reduction_qr'=>$qr]);

        $data['qr'] = $qr;

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-24 10:52
     * @功能说明:支付记录
     */

    public function payRecordList(){

        $input= $this->_input;

        $pay_record = new PayRecord();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','=',2];

        if(!empty($input['nickName'])){

            $dis[] = ['b.nickName','like','%'.$input['nickName'].'%'];

        }

        if(!empty($input['staff_name'])){

            $dis[] = ['c.name','like','%'.$input['staff_name'].'%'];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $input['start_time'] = $input['start_time']/1000;

            $input['end_time']   = $input['end_time']/1000;

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.pay_time','between',"$start_time,$end_time"];
        }


        $data = $pay_record->recordList($dis,$input['limit']);

        return $this->success($data);
    }






}
